package com.eightbears.beboy.ui.user.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.RefuntInfoAdapter;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.beboy.entity.OrderEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.socks.library.KLog;

import org.greenrobot.eventbus.EventBus;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_REFUND_ALIPAY;
import static com.eightbears.beboy.base.util.CommonAPI.URL_REFUND_WCHAT;
import static com.eightbears.beboy.ui.user.activity.OrderDetailActivity.PARAMS_ORDER_INFO;
import static com.eightbears.beboy.ui.user.activity.OrderDetailActivity.PARAMS_PAY_ORDER;

public class RefundActivity extends BaseActivity {

    public static final String EVENT_REFUND = "eventRefund";

    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.et_text)
    EditText et_text;
    @BindView(R.id.sv_root)
    ScrollView sv_root;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    private RefuntInfoAdapter mPaymentAdapter;
    private String orderId;
    private String channelType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refunt);

        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tv_title.setText("申请退款");
        OrderEntity.OrderInfo orderInfo = (OrderEntity.OrderInfo) getIntent().getExtras().get(PARAMS_ORDER_INFO);
        OrderEntity.OrderDetailEntity orderDetail = (OrderEntity.OrderDetailEntity) getIntent().getExtras().get(PARAMS_PAY_ORDER);

        if (null != orderInfo) {
            tv_name.setText(orderInfo.getTitle());
            tv_money.setText(orderInfo.getOrder().getReceipt_amount());
            orderId= orderInfo.getId();
            channelType = orderInfo.getOrder().getChannel();
        }

        if(null != orderDetail) {
            tv_name.setText(orderDetail.getData().getTitle());
            tv_money.setText( orderDetail.getData().getOrder().getReceipt_amount());
            orderId = orderDetail.getData().getId();
            channelType = orderDetail.getData().getOrder().getChannel();
        }
    }

    @Override
    protected void initData() {
        initAdapter();
    }

    @Override
    protected void initEvent() {
        mPaymentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mPaymentAdapter.checked(position);
                if (position == 3) {
                    et_text.setVisibility(View.VISIBLE);
                } else {
                    et_text.setVisibility(View.GONE);
                }
            }
        });
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.ll_back) void back(){
        finish();
    }

    @OnClick(R.id.tv_refunt_btn)
    void refunt() {
        sendRefunt();
    }

    private void sendRefunt() {
        String url;
        if (channelType.equals("0")) {
            url = URL_REFUND_ALIPAY;
        } else {
            url = URL_REFUND_WCHAT;
        }

        OkHttpUtils.post().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("id", orderId)
                .addParams("type", "4")
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                EventBus.getDefault().post(EVENT_REFUND);
                ShowToast.showShortToast(getString(R.string.alert_refund_submit));
                finish();
            }

            @Override
            public void onError(Call call, Exception e) {
                KLog.e(e.getMessage());
                Status_Error status_error = new Status_Error();
                ErrorUtils.alertError(RefundActivity.this, status_error);
            }

        });
    }



    private void initAdapter() {
        rv_list.setLayoutManager(new MyLinearLayoutManager(this));
        mPaymentAdapter = new RefuntInfoAdapter();
        List<String> items = Arrays.asList(getResources().getStringArray(R.array.refundList));
        mPaymentAdapter.setNewData(items);
        mPaymentAdapter.checked(0);
        rv_list.setAdapter(mPaymentAdapter);
    }
}
